from uuid import uuid4
from fastapi import Depends,Request,APIRouter
from app.utils.custom_route import CustomRoute
from app.utils.wxapp import WxappUtils
from app.utils.oss2_upload import Oss2UploadUtil
from app.utils.auth import get_current_user
from app.service.ai import AiService
from app.schema.ai import AiInfoSchema,AiUpdateSchema
from app.schema.auth import TokenData
from app.schema.base import ListFilterSchema,ListArgsSchema,ListOrderSchema,RespIdSchema,RespBaseSchema,RespDataSchema
from app.retriever.milvus import MilvusVectorStore
router = APIRouter(route_class=CustomRoute)
import time
@router.post('/create',response_model=RespBaseSchema)
async def  create(*,ai_info:AiInfoSchema,auth_data: TokenData = Depends(get_current_user)):
    return (await AiService(auth_data).create(ai_info))

@router.get('/info')
async def read(id: int,auth_data: TokenData = Depends(get_current_user)):
    try:
        aiService=AiService(auth_data)
        ai_info=aiService.read(id)
        data={
            "id":ai_info.id,
            "name":ai_info.name,
            "avatar":ai_info.avatar,
            "greet":ai_info.greet,
            "info":ai_info.info,
            "url":ai_info.url,
            "page":ai_info.page,
            "user_id":ai_info.user_id,
        }
        return RespDataSchema(data=data)
    except:
        return RespBaseSchema(code=50001,message='查询失败')

@router.post('/update')
async def update(*,ai_info:AiUpdateSchema,auth_data: TokenData = Depends(get_current_user)):
    try:
        aiService=AiService(auth_data)
        return aiService.update(ai_info)
    except BaseException as e:
        aiService.dao.db.sess.rollback()
        return RespBaseSchema(code=50001,message='更新失败')